//
//  HKHeaderCellTableViewCell.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2021/4/24.
//

#import "HKHeaderCellTableViewCell.h"
#import <Masonry/Masonry.h>
#import "UILabel+create.h"
#import "UIView+properties.h"
#import "HKUser.h"
#import "NSString+boundRect.h"
#import "HKLoadingView.h"
#import "HKCommonDefine.h"
#import "HKFontHelper.h"

static const CGFloat kKSSideMargin = 24.f;

static const CGFloat kKSTopImageH = 170.f;
static const CGFloat kKSContainerH = 113.f;

static const CGFloat kKSContainerTop = 99.f;

static const CGFloat kKSHeaderLeftMargin = 20.f;
static const CGFloat kKSHeaderTopMargin = 28.f;
static const CGSize kKSHeaderSize = {.width = 56, .height = 56};

static const CGFloat kKSTitleLeftMargin = 16.f;
static const CGFloat kKSTitleTopMargin = 36.f;

static const CGFloat kKSTitleH = 24.f;
static const CGFloat kKSDetailTitleH = 24.f;


@interface  HKHeaderCellTableViewCell ()

@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;

@end

@implementation HKHeaderCellTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
        self.backgroundColor = self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setupSubViews {
    [self.contentView addSubview:self.topImageView];
    
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.headerImageView];
    [self.containerView addSubview:self.nameLabel];
    [self.containerView addSubview:self.detailLabel];
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kKSTopImageH);
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(self.contentView);
    }];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kKSSideMargin);
        make.right.mas_equalTo(self.contentView).offset(-kKSSideMargin);
        make.height.mas_equalTo(kKSContainerH);
        make.top.mas_equalTo(kKSContainerTop);
    }];
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kKSHeaderLeftMargin);
        make.top.mas_equalTo(kKSHeaderTopMargin);
        make.size.mas_equalTo(kKSHeaderSize);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kKSTitleTopMargin);
        make.left.mas_equalTo(self.headerImageView.mas_right).offset(kKSTitleLeftMargin);
        make.right.mas_equalTo(-kKSTitleLeftMargin);
        make.height.mas_equalTo(kKSTitleH);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.nameLabel);
        make.height.mas_equalTo(kKSDetailTitleH);
        make.top.mas_equalTo(self.nameLabel.mas_bottom);
        make.left.mas_equalTo(self.nameLabel);
    }];
}

+ (CGFloat)cellHeightWithContent:(id)content {
    return 232;
}

- (void)updateContent:(id)content {
    HKUser *user = KSSAFE_CAST(content, HKUser);
    if (!user) return;
    [self.headerImageView setImage:[UIImage imageNamed:user.sex ? @"defaultFemale" : @"defaultMale"]];
    self.nameLabel.text = user.name;
    self.detailLabel.text = [NSString stringWithFormat:@"用户号：%@", user.number?:@""];
}

- (void)onTapHeader:(id)sender {
    if ([self.delegate respondsToSelector:@selector(headerCellTableViewCell:didTapHeaderImageView:)]) {
        [self.delegate headerCellTableViewCell:self didTapHeaderImageView:self.headerImageView];
    }
}

#pragma mark - properties
- (UIImageView *)topImageView {
    if (!_topImageView) {
        _topImageView = [UIImageView new];
        _topImageView.image = [UIImage imageNamed:@"profileBgImage"];
    }
    return _topImageView;
}

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.layer.cornerRadius = 10;
        _containerView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
        _containerView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0500].CGColor;
        _containerView.layer.shadowOffset = CGSizeMake(0,4);
        _containerView.layer.shadowOpacity = 1;
        _containerView.layer.shadowRadius = 8;
    }
    return _containerView;
}
- (UIImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [UIImageView new];
        _headerImageView.backgroundColor = [UIColor grayColor];
        _headerImageView.layer.cornerRadius = kKSHeaderSize.height / 2;
        _headerImageView.layer.masksToBounds = YES;
        _headerImageView.userInteractionEnabled = YES;
        _headerImageView.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapHeader:)];
        [_headerImageView addGestureRecognizer:tap];
    }
    return _headerImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel labelWithFont:[HKFontHelper appcnFontWithSize:18.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0x333333) textAligment:NSTextAlignmentLeft];
    }
    return _nameLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [UILabel labelWithFont:[HKFontHelper appcnFontWithSize:12.f]
                                    bgColor:nil
                                  textColor:UIColorFromRGB(0x828282) textAligment:NSTextAlignmentLeft];
    }
    return _detailLabel;
}

@end
